<?php

namespace App\Http\Controllers\Admin\Finance\Service;

use App\Http\Controllers\Admin\Finance\Repository\Account as AccountRepository;
use App\Http\Controllers\Admin\Finance\Repository\Commission;
use Illuminate\Http\Request;

class Account
{
    private $repository;
    private $member_id;
    private $type;
    private $request;

    const DISTRIBUTE =   1; //推荐奖
    const REBATE = 2;   //下级消费返利
    const FILL_INFORMATION = 3; //完善资料奖励
    const WITHDRAW = 4; //提现
    const RECHARGE_BY_COMBO = 5;    //套餐、活动充值
    const RECHARGE_BY_ADMIN = 6;    //后台充值
    const BUY_SCENE = 7;    //景点购买
    const REBATE_TOP = 8;   //团队奖（团队消费返利）
    const WITHDRAW_REFUSE = 9;//提现拒绝返回申请款


    const RECHARGE_ADMIN = 1;//后台充值
    const RECHARGE_COMBO = 2;//套餐充值
    const RECHARGE_ACTIVITY = 3;//活动充值

    public function __construct ()
    {
        $this->repository = new AccountRepository();
    }
    public function account_log($member_id,$type,$money,$options=[]){
        $desc = '';
        switch ($type){
            case self::DISTRIBUTE:
                $desc = '推广奖励';
                break;
            case self::REBATE:
                $desc = '下级用户消费奖励';
                break;
            case self::FILL_INFORMATION:
                $desc = '完善信息奖励('.config('wallet.wallet.wallet_spots').')';
                break;
            case self::WITHDRAW:
                $desc = '提现扣除'.config('wallet.wallet.wallet_push_money');
                break;
            case self::RECHARGE_BY_COMBO:
                $desc = '用户购买套餐';
                $desc .= '充值'.config('wallet.wallet.wallet_spots');
                break;
            case self::RECHARGE_BY_ADMIN:
                $desc = '后台充值'.config('wallet.wallet.wallet_spots');
                break;
            case self::BUY_SCENE:
                $desc = '浏览景点消耗'.config('wallet.wallet.wallet_spots');
                break;
            case self::REBATE_TOP:
                $desc = '团队消费奖励';
                break;
            case self::WITHDRAW_REFUSE:
                $desc = '提现拒绝返还'.config('wallet.wallet.wallet_push_money');

                break;

        }
        $model = $this->repository;
        $model->member_id = $member_id;
        $model->type = $type;
        $model->price = $money;
        $model->desc = $desc;
        return $model->save();
    }
    //常用参数操作
    /**
     * @param $status
     *
     * @return mixed
     */
    public function getTypeString($status){
        $statusList =  $this->getTypeList();
        return $statusList[$status];
    }


    /**
     * @return array
     */
    public static function getTypeList(){
        return [
            self::DISTRIBUTE =>  '推荐奖',
            self::REBATE =>  '上级提成',
            self::FILL_INFORMATION =>  '完善信息奖励',
            self::WITHDRAW =>  '提现',
            self::RECHARGE_BY_COMBO =>  '套餐充值',
            self::RECHARGE_BY_ADMIN =>  '后台充值',
            self::BUY_SCENE =>  '购买景点消耗',
            self::REBATE_TOP    =>  '团队提成',
            self::WITHDRAW_REFUSE   =>  '提现驳回'
        ];
    }
    /**
     * @param Request $request
     *
     * @return $this|\Illuminate\Database\Eloquent\Builder|static
     */
    public function getPageList(Request $request,$order=false){
        $this->request = $request;
        $model=$this->repository->with(['member'=>function($query) use ($request){
            if(isset($this->request->searchKey)){
                $query=$query->where('nickname','like', '%'.$this->request->searchKey.'%');
            }
        }]);

        //状态
        if(isset($this->request->type)&&$this->request->type!=''){
            $model=$model->where('type','=',$this->request->type);
        }
        //时间
        if(isset($this->request->searchTime)&&$this->request->searchTime!=''){
            //申请时间
            $timeArray=explode('~',$this->request->searchTime);
            $start_time=trim($timeArray[0]);
            $end_time=trim($timeArray[1]);
            $searchTimeType = isset($this->request->searchTimeType)?$this->request->searchTimeType:1;
            $model=$model->whereBetween('created_at',[$start_time,$end_time]);
        }
        if($order==true){
            $model = $model->orderBy('sort','ASC');
        }
        $model=$model->paginate($this->request->limit);

        if($model){
            $model=$model->toArray();
        }
        foreach ($model['data'] as $k=>&$v){
            $v['type_name'] = self::getTypeString($v['type']);
            $v['nickname'] = $v['member']['nickname'];

        }
        return $model;
    }

}
